package com.xx.bysj.security;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xx.bysj.utils.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Component
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Autowired
    private ObjectMapper mapper;

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
                                        AuthenticationException exception) throws IOException, ServletException {
        JSONObject returnObj = new JSONObject();
        try {
            returnObj.put("code", NumberUtils.FOUR_HUNDRED);
            returnObj.put("message","账号或者密码有误！" );
        } catch (JSONException e1) {
            throw new RuntimeException(e1);
        }
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().print(returnObj.toString());
        response.getWriter().flush();
    }
}
